package com.yanqu.road.web.controller.game;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.logic.bussiness.player.UserBussiness;
import com.yanqu.road.server.helper.SignHelper;
import com.yanqu.road.server.manger.AppKeyMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.string.StringUtils;
import com.yanqu.road.web.WebServer;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import com.yanqu.road.web.controller.chargecenter.RechargeConditionResult;
import com.yanqu.road.webservice.invoke.GameWebServiceInvoke;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@WebServlet(urlPatterns = "/h5QueryUser")
public class H5QueryUser extends BaseJsonHttpServlet {

	private static Logger logger = LogManager.getLogger(H5QueryUser.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("request queryUser");
		HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
		RechargeConditionResult result = new RechargeConditionResult();

		JSONObject paramObj = httpJsonWebParam.getParamObj();
		JSONObject resultObj;
		String platformUserId;
		String sign;
		try{
			platformUserId = paramObj.getString("uid");
			sign = paramObj.getString("sign");
		}catch (Exception e){
			logger.error("queryUser argument error:" + e);
			sendMessage(response, JSON.toJSONString(result));
			return;
		}
		try{
			SortedMap<String, String> paramMap = SignHelper.getParamMap(paramObj);
			/*if(!SignHelper.checkSign(paramMap, AppKeyMgr.getAndroidAppSecret(), sign)){
				logger.error("queryUser sign error");
				sendResult(response, 0, "sign error");
				return;
			}*/
			result.setState(1);
			logger.info("queryUser 请求成功");
			List<UserInfo> list = UserBussiness.getUserInfoByPlatformUserId(platformUserId);
			if(list.isEmpty()){
				logger.info("uid {} queryUser no roles",platformUserId);

			}
			List<Map<String, Object>> dataList = new ArrayList<>();
			for (UserInfo info : list) {
				Map<String, Object> data = new HashMap<>();
				data.put("dsid", WebServer.getInstance().getServerId());
				data.put("dsname", ServerListMgr.getServerInfo(WebServer.getInstance().getServerId()).getServerName());
				data.put("drid", info.getUserId());
				data.put("drname", info.getNickName());
				data.put("drlevel", info.getTitleId());
				data.put("drtype", info.getPhonePlatform());
				dataList.add(data);
			}
			result.setData(dataList);
			sendMessage(response, JSON.toJSONString(result));
		} catch (Exception e) {
			logger.error("queryUser 参数错误:" + e);
			sendMessage(response, JSON.toJSONString(result));
		}
	}
}
